<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Light Weight Templating With Resource Templates</title>
  </head>

  <body>
    <h1>Light Weight Templating With Resource Templates</h1>

<h2>Overview</h2>

<p>While high-level templating systems can be used with Twisted (for
example, <a href="https://launchpad.net/nevow">Divmod
Nevow</a>, sometimes one needs a less file-heavy system which lets one
directly write HTML. While 
<code class="API" base="twisted.web.script">ResourceScript</code> is
available, it has a high coding overhead, and requires some boring string
arithmetic. 
<code class="API" base="twisted.web.script">ResourceTemplate</code> fills the
space between Nevow and ResourceScript using Quixote's PTL (Python Templating
Language).</p>

<p>ResourceTemplates need Quixote
installed. In <a href="http://www.debian.org">Debian</a>, that means
installing the <code>python-quixote</code> package
(<code>apt-get install python-quixote</code>). Other operating systems
require other ways to install Quixote, or it can be done manually.</p>

<h2>Configuring Twisted Web</h2>

<p>The easiest way to get Twisted Web to support ResourceTemplates is to
bind them to some extension using the web tap's <code>--processor</code>
flag. Here is an example:</p>

<pre>
% twistd web --path=/var/www \
        --processor=.rtl=twisted.web.script.ResourceTemplate
</pre>

<p>The above command line binds the <code>rtl</code> extension to use the 
ResourceTemplate processor. Other ways are possible, but would require
more Python coding and are outside the scope of this HOWTO.</p>

<h2>Using ResourceTemplate</h2>

<p>ResourceTemplates are coded in an extension of Python called the
<q>Python Templating Language</q>. Complete documentation of the PTL
is available
at <a href="http://quixote.python.ca/quixote.dev/doc/PTL.html">the 
quixote web site</a>. The web server will expect the PTL source file
to define a variable named <code>resource</code>.  This should be
a <code class="API">twisted.web.resource.Resource</code>,
whose <code>.render</code> method be called. Usually, you would want
to define <code>render</code> using the keyword <code>template</code>
rather than <code>def</code>.</p>

<p>Here is a simple example for a resource template.</p>

<a href="listings/webquote.rtl" class="py-listing">Resource Template for Quotes</a>

</body>
</html>
